package com.booter.provider;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * create table ureport_file_tbl (
 * id_ int primary key auto_increment,
 * name_ varchar(100) not null,
 * content_ mediumblob,
 * create_time_ timestamp default now(),
 * update_time_ timestamp default now()
 * )
 *
 * @author xueqiu
 * @date 2023/3/23 11:00
 */
public class DatabaseReportProvider implements ReportProvider {
    private static final String NAME = "mysql-provider";

    //特定前缀，ureport底层调用getPrefix方法来获报表操作的Provider类
    private String prefix = "mysql:";

    //是否禁用
    private boolean disable = false;

//    @Autowired
//    UreportFileMapper ureportFileMapper;

    //加载表报
    @Override
    public InputStream loadReport(String fileName) {
//        UreportFileEntity ureportFileEntity = ureportFileMapper.queryUreportFileEntityByName(fileName);
//        byte[] content = new byte[0];
//        if (ureportFileEntity != null) {
//            content = ureportFileEntity.getContent();
//        }
//        return new ByteArrayInputStream(content);
        return null;
    }

    //删除表报
    @Override
    public void deleteReport(String fileName) {
        if (fileName != null) {
            fileName = getNoCorrectName(fileName);
            //ureportFileMapper.deleteReportFileByName(fileName);
        }
    }

    //查询所有表报
    @Override
    public List<ReportFile> getReportFiles() {
        List<ReportFile> files = new ArrayList<>();
        //files.add(new ReportFile("fileName", new Date()));
        return files;
    }

    //保存表报
    @Override
    public void saveReport(String fileName, String content) {
        fileName = getNoCorrectName(fileName);
        Date date = new Date();
//        UreportFileEntity ureportFileEntity = ureportFileMapper.queryUreportFileEntityByName(fileName);
//        if (ureportFileEntity == null) {
//            ureportFileEntity = new UreportFileEntity();
//            ureportFileEntity.setContent(content.getBytes());
//            ureportFileEntity.setUpdateTime(date);
//            ureportFileEntity.setCreateTime(date);
//            ureportFileEntity.setName(fileName);
//            ureportFileMapper.insertReportFile(ureportFileEntity);
//        } else {
//            ureportFileEntity.setContent(content.getBytes());
//            ureportFileEntity.setUpdateTime(date);
//            ureportFileMapper.updateReportFile(ureportFileEntity);
//        }
    }


    /**
     * 获取没有前缀的文件名
     *
     * @param name
     * @return
     */
    private String getNoCorrectName(String name) {
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length());
        }
        return name;

    }


    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean disabled() {
        return disable;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }
}
